//
//  ZHScrollViewLB.m
//  ScrollView——test
//
//  Created by 邹浩 on 15/5/6.
//  Copyright (c) 2015年 蓝鸥. All rights reserved.
//

#import "ZHScrollViewLB.h"
#import <SDWebImage/UIImageView+WebCache.h>


@interface ZHScrollViewLB ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIImageView *nextImageV;
@property (nonatomic, strong)UIImageView *firstImageV;
@property (nonatomic, strong)UIImageView *currentImageV;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, assign)BOOL isScroll;
@property (nonatomic, assign)NSInteger index;
@property (nonatomic, strong)UIPageControl *pageC;



@end

@implementation ZHScrollViewLB

- (instancetype)initWithFrame:(CGRect)frame WithImageName:(NSArray *)imageNames WithTime:(NSInteger)time
{
    
    if (self = [super initWithFrame:frame]) {
        self.imageArray = imageNames;
        self.time = time;
        [self setTheProfile];
    }
    return self;
}

- (void)setTheProfile
{
    self.contentOffset = CGPointMake(self.bounds.size.width, 0);
    self.delegate = self;
    self.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
    self.showsHorizontalScrollIndicator = NO;
    self.pagingEnabled = YES;
    self.bounces = NO;
    
//    self.currentImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index] ofType:nil inDirectory:nil]];
    [self.currentImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]] placeholderImage:[UIImage imageNamed:@""]];
    [self.firstImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray.lastObject] placeholderImage:[UIImage imageNamed:@""]];
    [self.nextImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index+1]] placeholderImage:[UIImage imageNamed:@""]];
//    self.currentImageV.image = [UIImage imageNamed:self.imageArray[0]];
//    self.firstImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray.lastObject ofType:nil inDirectory:nil]];
//    self.nextImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index + 1] ofType:nil inDirectory:nil]];
    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.time target:self selector:@selector(updateTheScrollView:) userInfo:nil repeats:YES];
//    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    self.pageC.currentPage = 0;
    self.pageC.numberOfPages = self.imageArray.count;
    [self.pageC.layer setCornerRadius:8];
//    [self.pageC addTarget:self action:@selector(changeThePage) forControlEvents:UIControlEventValueChanged];
    
    
}

- (void)updateTheScrollView:(NSTimer *)timer
{
    if (self.isScroll) {
        NSLog(@"%d", self.isScroll);
        return;
    }
    CGPoint offSet = self.contentOffset;
    offSet.x += offSet.x;
    NSLog(@"########%f", offSet.x);
    [self setContentOffset:offSet animated:YES];
    
    if (offSet.x >= self.bounds.size.width * 2) {
        offSet.x = self.bounds.size.width;
    }
    
   
    
}

- (void)changeThePage
{
    
}


- (UIImageView *)currentImageV
{
    if (_currentImageV == nil) {
        _currentImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        [self addSubview:_currentImageV];
    }
    return _currentImageV;
}

- (UIImageView *)nextImageV
{
    if (_nextImageV == nil) {
        _nextImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * 2, 0, self.bounds.size.width, self.bounds.size.height)];
        [self addSubview:_nextImageV];
    }
    return _nextImageV;
}

- (UIImageView *)firstImageV
{
    if (_firstImageV == nil) {
        _firstImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0, self.bounds.size.width, self.bounds.size.height)];
        [self addSubview:_firstImageV];
        
    }
    return _firstImageV;
}

- (UIPageControl *)pageC
{
    if (_pageC == nil) {
        _pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(self.bounds.size.width, self.frame.size.height - 30, self.frame.size.width, 30)];
        [self.superview addSubview:_pageC];
        
    }
    return _pageC;
}

//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
//{
//    if(self.contentOffset.x < self.bounds.size.width) {
//        if (self.index == 0) {
//            self.index = self.imageArray.count-1;
//        } else {
//            self.index--;
//        }
//    }
//    if (self.contentOffset.x > self.bounds.size.width) {
//        if (self.index == self.imageArray.count - 1) {
//            self.index = 0;
//        } else {
//            self.index++;
//        }
//    }
//    
//    
//    self.contentOffset = CGPointMake(self.bounds.size.width, 0);
////    self.currentImageV.image = [UIImage imageNamed:self.imageArray[self.index]];
//    self.currentImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index] ofType:nil inDirectory:nil]];
//    if (self.index == 0) {
//        self.firstImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.imageArray.count-1] ofType:nil inDirectory:nil]];
//    }else {
//        self.firstImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index-1] ofType:nil inDirectory:nil]];
//    }
//    if (self.index == self.imageArray.count - 1) {
//        self.nextImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[0] ofType:nil inDirectory:nil]];;
//    } else {
//        self.nextImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index+1] ofType:nil inDirectory:nil]];;
//    }
//    
//    self.pageC.currentPage = self.index;
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(self.contentOffset.x < self.bounds.size.width) {
        if (self.index == 0) {
            self.index = self.imageArray.count-1;
        } else {
            self.index--;
        }
    }
    if (self.contentOffset.x > self.bounds.size.width) {
        if (self.index == self.imageArray.count - 1) {
            self.index = 0;
        } else {
            self.index++;
        }
    }
    
    _isScroll = NO;
    self.contentOffset = CGPointMake(self.bounds.size.width, 0);
//    self.currentImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index] ofType:nil inDirectory:nil]];
    [self.currentImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]] placeholderImage:[UIImage imageNamed:@""]];
    if (self.index == 0) {
//        self.firstImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.imageArray.count - 1] ofType:nil inDirectory:nil]];
        [self.firstImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray.lastObject] placeholderImage:[UIImage imageNamed:@""]];
        
    }else {
        [self.firstImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index-1]] placeholderImage:[UIImage imageNamed:@""]];
//        self.firstImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index - 1] ofType:nil inDirectory:nil]];
    }
    if (self.index == self.imageArray.count - 1) {
        [self.nextImageV sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]] placeholderImage:[UIImage imageNamed:@""]];
//        self.nextImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[0] ofType:nil inDirectory:nil]];
    } else {
        self.nextImageV.image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.imageArray[self.index + 1] ofType:nil inDirectory:nil]];
    }
    NSLog(@"++++%@", self.currentImageV.image);
    self.pageC.currentPage = self.index;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    _isScroll = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        NSLog(@"-------%ld", self.index);
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
